package co.edu.javeriana.konigsberg.xavier.gui.bean;


import java.io.Serializable;
import java.math.BigInteger;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import co.edu.javeriana.konigsberg.xavier.dto.UsuarioDTO;

@ManagedBean(name="managerBean")
public class ManagerBean implements Serializable{
	private static final long serialVersionUID = 1L;

	@PostConstruct
	public void init(){
		
	}
	
	public void addMessageInfo(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", message));
	}
	public void addMessageWarn(String message) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning!", message));
    }
     
    public void addMessageError(String message) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", message));
    }
     
    public void addMessageFatal(String message) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Fatal!", message));
    }
    
    public void cargarEnSession(String nombre, Object object){
    	FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
        session.setAttribute(nombre, object);
    }
    
    public Object obtenerEnSession(String nombre){
    	FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(true);
        return session.getAttribute(nombre);
    }
    
    public UsuarioDTO obtenerUsuario(){
    	return (UsuarioDTO)obtenerEnSession("usuario");
    }
    
    public BigInteger obtenerIdUsuario(){
    	return obtenerUsuario().getIdUsuario();
    }
}
